-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify Buffer hex string error message #4877
Conversation
I don't think we have a regression test for this. Can you add one to Apropos the commit log, can you make sure it conforms to the guidelines from CONTRIBUTING.md? Thanks. |
src/node_buffer.cc
Outdated
@@ -623,7 +623,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) { | |||
Local<String> str = args[0]->ToString(env->isolate()); | |||
|
|||
if (encoding == HEX && str->Length() % 2 != 0) | |||
return env->ThrowTypeError("Invalid hex string"); | |||
return env->ThrowTypeError("Invalid hex string length (must be even)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, "The hex string length must be an even number" or "The hex string must have an even number of characters"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
Marking |
@bnoordhuis I added a simple test that looks for the TypeError and (new) message. |
Can you run |
@@ -623,7 +623,8 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) { | |||
Local<String> str = args[0]->ToString(env->isolate()); | |||
|
|||
if (encoding == HEX && str->Length() % 2 != 0) | |||
return env->ThrowTypeError("Invalid hex string"); | |||
return env->ThrowTypeError("Hex strings must have an even number of " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis does our C++ style allow strings split like this (the linter doesn't complain)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should be fine.
@cjihrig Did a |
The commit message should be LGTM, but maybe @trevnorris wants to sign off on this? |
@cjihrig : Fixed. I was going off the commit log, which seemed to have both. Sorry about that. |
Looks like a commit just landed that conflicts with this one. Mind rebasing? |
No problem. Let me know if that works for you. |
Is this the only location we throw for incorrect hex string length? If so then LGTM. |
test/parallel/test-buffer.js
Outdated
new Buffer('8', 'hex'); | ||
}, function(err) { | ||
return err instanceof TypeError && | ||
err.message === 'The hex string must have an even number of characters'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: /^The hex string must have an even number of characters/.test(err.message)
|
@jgeewax ... ping. There were a few nits that need to be looked at. |
be3d46b
to
df3fe4f
Compare
OK -- Addressed the console.log stuff as well as the nit for regex checking vs equality. |
|
test/parallel/test-buffer.js
Outdated
@@ -734,6 +734,14 @@ for (let i = 0; i < 256; i++) { | |||
assert.equal(hexb2[i], hexb[i]); | |||
} | |||
|
|||
// Create buffer from odd-length hex string should fail. | |||
assert.throws(function() { | |||
new Buffer('8', 'hex'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: can you update this to use Buffer.from('8', 'hex')
Taking a clue from https://github.com/nodejs/node/blob/master/src/string_bytes.cc#L503, provide more reason for why a hex string is invalid (namely that the issue is the length being odd rather than even).